package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.admin.mapper.AdChannelMapper;
import com.usian.admin.service.AdChannelService;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {


    @Override
    public ResponseResult findByNameAndPage(ChannelDto channelDto) {

        //校验参数
        if(channelDto ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        channelDto.checkParam();


        //mybatisplus 条件构造器查询列表

        Page pageparam =new Page(channelDto.getPage(),channelDto.getSize());
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper =new LambdaQueryWrapper<>();

        if(StringUtils.isNotBlank(channelDto.getName())){
            lambdaQueryWrapper.like(AdChannel::getName,channelDto.getName());
        }
        if(channelDto.getStatus()!=null){
            lambdaQueryWrapper.eq(AdChannel::getStatus,channelDto.getStatus()); //有效
        }

        IPage result = page(pageparam, lambdaQueryWrapper);

        PageResponseResult responseResult =new PageResponseResult(channelDto.getPage(),channelDto.getSize(),(int)result.getTotal());
        responseResult.setData(result.getRecords());


        return responseResult;
    }

    @Override
    public ResponseResult saveChannel(AdChannel adChannel) {
        //校验参数
        if(adChannel ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        adChannel.setCreatedTime(new Date());
        this.save(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult updateChannel(AdChannel adChannel) {
        //校验参数
        if(adChannel ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        this.updateById(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        //校验参数
        if(id ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        this.removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
